<?php
/**
* SocialEngine
*
* @category   Application_Extensions
* @package    Flashgame
* @copyright 2010 YouNet
* @license    http://www.modules2buy.com/license/
* @version    $Id: AdminSettingsController.php 6527 2010-06-23 02:03:14Z YouNet $
* @author     ToAnh
*/

/**
* @category   Application_Extensions
* @package    Flashgame
* @copyright 2010 YouNet
* @license    http://www.socialengine.net/license/
*/
class Flashgame_AdminCategoriesController extends Core_Controller_Action_Admin
{


    public function categoriesAction()
    {
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
        ->getNavigation('flashgame_admin_main', array(), 'flashgame_admin_main_categories');

        $this->view->categories = Engine_Api::_()->flashgame()->getCategories();
    }

    public function addCategoryAction()
    {
        // In smoothbox
        $this->_helper->layout->setLayout('admin-simple');

        // Generate and assign form
        $form = $this->view->form = new Flashgame_Form_Admin_Category();
        $form->setAction($this->getFrontController()->getRouter()->assemble(array()));
        // Check post
        if( $this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost()) )
        {
            // we will add the category
            $values = $form->getValues();

            $db = Engine_Db_Table::getDefaultAdapter();
            $db->beginTransaction();

            try
            {
                // add category to the database
                // Transaction
                $table = Engine_Api::_()->getDbtable('categories', 'flashgame');

                // insert the flashgame category entry into the database
                $row = $table->createRow();
                //$row->user_id   =  1;
                $row->category_name = $values["label"];
                $row->save();

                // change the category of all the flashgames using that category

                $db->commit();
            }

            catch( Exception $e )
            {
                $db->rollBack();
                throw $e;
            }
            $this->_forward('success', 'utility', 'core', array(
            'smoothboxClose' => 10,
            'parentRefresh'=> 10,
            'messages' => array('')
            ));
        }

        // Output
        $this->renderScript('admin-categories/form.tpl');
    }

    public function deleteCategoryAction()
    {
        // In smoothbox
        $this->_helper->layout->setLayout('admin-simple');
        $id = $this->_getParam('id');
        $this->view->flashgame_id=$id;
        // Check post
        if( $this->getRequest()->isPost())
        {
            $db = Engine_Db_Table::getDefaultAdapter();
            $db->beginTransaction();

            try
            {
                // go through logs and see which flashgame used this category and set it to ZERO
                $flashgameTable = Engine_Api::_()->getDbtable('flashgames', 'flashgame');
                $select = $flashgameTable->select()->where('category_id = ?', $id);
                $flashgames = $flashgameTable->fetchAll($select);

                // create permissions
                foreach( $flashgames as $flashgame )
                {
                    //this is not working
                    $flashgame->category_id = 0;
                    $flashgame->save();
                }

                $row = Engine_Api::_()->flashgame()->getCategory($id);
                // delete the flashgame category in the database
                $row->delete();

                $db->commit();
            }

            catch( Exception $e )
            {
                $db->rollBack();
                throw $e;
            }
            $this->_forward('success', 'utility', 'core', array(
            'smoothboxClose' => 10,
            'parentRefresh'=> 10,
            'messages' => array('')
            ));
        }

        // Output
        $this->renderScript('admin-categories/delete.tpl');
    }

    public function editCategoryAction()
    {
        // In smoothbox
        $this->_helper->layout->setLayout('admin-simple');
        $form = $this->view->form = new Flashgame_Form_Admin_Category();
        $form->setAction($this->getFrontController()->getRouter()->assemble(array()));

        // Check post
        if( $this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost()) )
        {
            // Ok, we're good to add field
            $values = $form->getValues();

            $db = Engine_Db_Table::getDefaultAdapter();
            $db->beginTransaction();

            try
            {
                // edit category in the database
                // Transaction
                $row = Engine_Api::_()->flashgame()->getCategory($values["id"]);

                $row->category_name = $values["label"];
                $row->save();
                $db->commit();
            }

            catch( Exception $e )
            {
                $db->rollBack();
                throw $e;
            }
            $this->_forward('success', 'utility', 'core', array(
            'smoothboxClose' => 10,
            'parentRefresh'=> 10,
            'messages' => array('')
            ));
        }

        // Must have an id
        if( !($id = $this->_getParam('id')) )
        {
            throw new Zend_Exception('No identifier specified');
        }

        // Generate and assign form
        $category = Engine_Api::_()->flashgame()->getCategory($id);
        $form->setField($category);

        // Output
        $this->renderScript('admin-categories/form.tpl');
    }
}